Public Class ThreeCalendar
    Dim DoChange As Boolean = True
    Function getBeforeDate(ByVal CurrentDate As Date, ByVal calendarname As String) As Date
        DoChange = False
        Dim is31 As Boolean = False
        Dim DateAfter As Date
        Dim DateBefore As Date
        Dim vCurrDay As Integer = DatePart(DateInterval.Day, CurrentDate)
        Dim vCurrMoth As Integer = DatePart(DateInterval.Month, CurrentDate)
        Dim vCurrYear As Integer = DatePart(DateInterval.Year, CurrentDate)
        ''Dim DaysArr() As String = GetDaysBeforeAfter(vCurrDay, vCurrMoth).Split(",")
        If vCurrMoth = 1 And vCurrDay = 1 Then
            DateAfter = CDate("2/" & vCurrDay & "/" & vCurrYear)
            DateBefore = CDate("12/" & vCurrDay & "/" & vCurrYear - 1)
        Else
            If vCurrMoth = 12 And vCurrDay = 31 Then
                DateAfter = CDate("1/" & vCurrDay & "/" & vCurrYear + 1)
                DateBefore = CDate("11/" & vCurrDay - 1 & "/" & vCurrYear)
            Else
                If vCurrMoth = 12 Then
                    DateAfter = CDate(1 & "/" & vCurrDay & "/" & vCurrYear + 1)
                Else
                    DateAfter = CDate(vCurrMoth + 1 & "/" & vCurrDay & "/" & vCurrYear)
                End If

                If vCurrMoth = 1 Then
                    DateBefore = CDate(12 & "/" & vCurrDay & "/" & vCurrYear - 1)
                Else
                    DateBefore = CDate(vCurrMoth - 1 & "/" & vCurrDay & "/" & vCurrYear)
                End If

            End If
        End If

        Me.mcCurrent.SetDate(CurrentDate)
        Me.mcAfter.SetDate(DateAfter)
        Me.mcBefore.SetDate(DateBefore)

        DoChange = True
    End Function
    Function getMothDays(ByVal CurrentMoth As Integer) As Boolean
        Dim vMothw31() As String = "1,3,5,7,8,10,12".Split(",")
        Dim i As Integer
        For i = 0 To vMothw31.Length - 1
            If vMothw31(i) = CurrentMoth Then
                Return True
                Exit For
            End If
        Next
        Return False
    End Function
    Function GetDaysBeforeAfter(ByVal CurrDay As Integer, ByVal CurrentMonth As Integer) As String
        Dim arrDays As String = Nothing
        If getMothDays(CurrentMonth) = True Then
            If CurrDay = 31 Then
                arrDays = "1,30"
            Else
                If CurrDay = 1 Then
                    arrDays = "2,30"
                Else
                    arrDays = CurrDay - 1 & "," & CurrDay + 1
                End If
            End If
        Else
            If CurrDay = 30 Then
                arrDays = "1,29"
            Else
                If CurrDay = 1 Then
                    arrDays = "2,30"
                Else
                    arrDays = CurrDay + 1 & "," & CurrDay - 1
                End If
            End If
        End If
        Return arrDays
    End Function
    Private Sub ThreeCalendar_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        getBeforeDate(Now, "Current")
        Me.mcCurrent.MaxSelectionCount = 1
        Me.mcBefore.MaxSelectionCount = 1
        Me.mcAfter.MaxSelectionCount = 1
    End Sub
    Private Sub mcAfter_DateChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles mcAfter.DateChanged
        If DoChange = True Then
            getBeforeDate(mcAfter.SelectionRange.Start, "After")
        End If
    End Sub
    Private Sub mcCurrent_DateChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles mcCurrent.DateChanged
        If DoChange = True Then
            getBeforeDate(mcCurrent.SelectionRange.Start, "Current")
        End If

    End Sub
    Private Sub mcBefore_DateChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles mcBefore.DateChanged
        If DoChange = True Then
            getBeforeDate(mcBefore.SelectionRange.Start, "Before")
        End If
    End Sub
End Class
